package history

import (
	"strconv"
	"strings"
)

type TreeNode1 struct {
	Val   int
	Left  *TreeNode1
	Right *TreeNode1
}
type Codec struct {
	Node *TreeNode1
	Vals []int
}

func ConstructorTreeNode() Codec {
	return Codec{}
}

// Serializes a tree to a single string.
func (this *Codec) serialize(root *TreeNode1) string {
	this.Node = root
	this.Vals = preSearchDfs(root, []int{})
	var str string
	for i := 0; i < len(this.Vals); i++ {
		str += strconv.Itoa(this.Vals[i]) + "#"
	}
	return str
}

// Deserializes your encoded data to tree.
func (this *Codec) deserialize(data string) *TreeNode1 {
	if data == "" {
		return nil
	}
	var vals = strings.Split(data, "#")
	if len(vals) <= 0 {
		return nil
	}
	num0, _ := strconv.Atoi(vals[0])
	var root = TreeNode1{
		Val: num0,
	}
	for i := 0; i < len(vals); i++ {
		// 5 4 nil nil 6
	}
	return &root
}

func preSearchDfs(node *TreeNode1, res []int) []int {
	if node == nil {
		res = append(res, 000)
		return res
	}
	res = append(res, node.Val)
	res = preSearchDfs(node.Left, res)
	res = preSearchDfs(node.Right, res)
	return res
}

/**
 * Your Codec object will be instantiated and called as such:
 * ser := Constructor();
 * deser := Constructor();
 * data := ser.serialize(root);
 * ans := deser.deserialize(data);
 */
